package com.koo.framework.config;

import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.secret.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.util.Arrays;

@Slf4j
public class ThirdPropFileConfig {
    private static final String SYS_CONFIG_FILE_NAME = "application.yml";
    private static PropertiesConfiguration config;

    static {
        try {
            config = new PropertiesConfiguration(SYS_CONFIG_FILE_NAME);
        } catch (ConfigurationException var2) {
            log.error("加载配置文件" + SYS_CONFIG_FILE_NAME + "失败", var2.getMessage());
        }
    }

    public static String getProperty(String key) {
        return getProperty(key, "");
    }

    public static String getProperty(String key, String defaultStr) {
        String value = defaultStr;
        if (config.containsKey(key)) {
            value = config.getString(key);
        }

        if (MyStrUtil.isNotEmpty(value) && value.length() >= 32 && MyStrUtil.isUpperCase(value) && "true".equals(config.getProperty("encrypt"))) {
            value = AESUtil.decrypt(value);
        }

        return value != null && value.length() != 0 ? value : defaultStr;
    }

    public static int getIntProp(String key) {
        return getIntProp(key, 0);
    }

    public static int getIntProp(String key, int defaultValue) {
        return MyStrUtil.toInt(getProperty(key), defaultValue);
    }

    public static String getPropertys(String key) {
        String[] value = getPropertyArray(key);
        return value == null ? "" : Arrays.toString(value).replace("[", "").replace("]", "");
    }

    public static String[] getPropertyArray(String key) {
        String[] value = null;
        if (config.containsKey(key)) {
            value = config.getStringArray(key);
        }

        if (value != null && value.length > 0 && "true".equals(config.getProperty("encrypt"))) {
            String[] strArr = new String[value.length];

            for (int i = 0; i < value.length; ++i) {
                if (MyStrUtil.isNotEmpty(value[i]) && value[i].length() >= 32 && MyStrUtil.isUpperCase(value[i])) {
                    strArr[i] = AESUtil.decrypt(value[i]);
                } else {
                    strArr[i] = value[i];
                }
            }
            value = strArr;
        }
        return value == null ? new String[0] : value;
    }

    public static void removeProperty(String key) {
        config.clearProperty(key);
    }

    public static void setProperty(String key, String value) {
        if (MyStrUtil.isNotEmpty(value) && "true".equals(config.getProperty("encrypt"))) {
            String[] strArr = value.split(",");
            StringBuffer sBuffer = new StringBuffer();
            int len = strArr.length;

            for (int i = 0; i < len; ++i) {
                String string = strArr[i];
                sBuffer.append("," + AESUtil.encrypt(string));
            }

            sBuffer.deleteCharAt(0);
            value = sBuffer.toString();
        }

        config.setProperty(key, value);
    }

    public static void saveChange() {
        try {
            config.save();
        } catch (ConfigurationException var1) {
            log.error("保存配置", var1);
        }
    }


}
